const { Md5Crypto } = require('../util/crypto')
const { signToken } = require('../util/token')
const { getAdmin, addAdmin, verifyAdmin,changePassword,changeStatus,editAdmin } = require('../dao/admin')

// 登录
function AdminLogin(params) {
    return new Promise(async (resolve, reject) => {
        const { account, password} = params
        // 账号登录
            if (!account || !password) {
                resolve(fail('未传递账号或密码'))
                return
            }
        verifyAdmin({ account, password: Md5Crypto(password, account) }).then(admin => {
                if (!admin) {
                    resolve(fail('账号或密码错误'))
                } else {
                    const { admin_id } = admin
                    signToken({ admin_id }).then(token => {
                        delete admin['password']
                        resolve(ok({
                            data: { token, info: admin }
                        }))
                    }).catch(err => {
                        resolve(error(`服务异常:${err.message}`))
                    })
                }
            }).catch(err => {
                resolve(error(err))
            })
    })
}

//注册新管理员
function AdminRegister(params) {
    return new Promise((resolve, reject) => {
        const { account, password,work_num,name } = params
        //console.log(account,password)
        if (!account || !password) {
            resolve(fail('未传递账号或密码'))
            return
        }
        if (!work_num) {
            resolve(fail('未传递工号'))
            return
        }
        if (!name) {
            resolve(fail('未传递姓名'))
            return
        }
            addAdmin({ account, password: Md5Crypto(password, account) ,work_num,name}).then(() => {
                resolve(ok())
            }).catch(err => {
                resolve(error(err))
            })
        })
}

//管理员修改密码
function AdminChangePassword(params){
    return new Promise((resolve, reject) => {
        const {admin_id,password,new_password,account}=params
        if(!admin_id){
            resolve(fail('未传递管理员id,有可能需要重新登陆'))
            return
        }
        if(!password){
            resolve(fail('未传递旧密码'))
            return
        }
        if(!new_password){
            resolve(fail('未传递新密码'))
            return
        }
        verifyAdmin({account,password: Md5Crypto(password,account)}).then(admin => {
            if (!admin) {
                resolve(fail('账号或密码错误'))
            }else{
                //  console.log(user_id)
                changePassword({admin_id,password: Md5Crypto(new_password,account)}).then(()=>{
                    resolve(ok({msg:"密码修改成功"}))
                }).catch(err => {
                    resolve(error(err))
                })
            }
        })
    })
}

//退出登录
function AdminLogout(params) {
    return new Promise((resolve, reject) => {
        const {admin_id} = params
        if(!admin_id){
            resolve(fail('未传递用户id,有可能需要重新登陆'))
            return
        }
        changeStatus({admin_id}).then(()=>{
          resolve(ok({msg:"退出登录成功"}))
      }).catch(err => {
          resolve(error(err))
     })

    })
}

// 修改管理员信息
function AdminEdit(params) {
    return new Promise((resolve, reject) => {

        // 调用 AdminUpdate 函数
        editAdmin(params)
            .then(data => {
                resolve(ok({ data, msg: '管理员信息更新成功' }));
            })
            .catch(err => {
                resolve(error(err));
            });
    });
}

// 获取管理员信息
function AdminGetInfo(params) {
    return new Promise((resolve, reject) => {
        getAdmin(params)
            .then(data => {
                resolve(ok({ data: { name: data.name, avatar: data.avatar, sex: data.sex, phone_num: data.phone_num, qq: data.qq } }));
            })
            .catch(err => {
                resolve(error(err))
            });
    })
}

module.exports = {
    AdminLogin,AdminRegister,AdminChangePassword,AdminLogout,AdminEdit,AdminGetInfo
}